<!-- 地区选择器 -->
<script setup>
import { onClickOutside } from "@vueuse/core";
import { computed, reactive, ref } from "vue";
import { area } from "@/api/constants";

const emits = defineEmits(["change"]);
const show = ref(false);
const target = ref(null);
const cityData = ref([]);

cityData.value.push(...area);

onClickOutside(target, (event) => {
  show.value = false;
});

defineProps({
  fullLocation: {
    type: String,
    default: "",
  },
  placeholder: {
    type: String,
    default: "",
  },
});

const open = () => {
  show.value = true;
  for (const key in changeResult) {
    changeResult[key] = "";
  }
};

const changeResult = reactive({
  provinceCode: "",
  provinceName: "",
  cityCode: "",
  cityName: "",
  countyCode: "",
  countyName: "",
  fullLocation: "",
});

// 当你点击按钮的时候记录
const changeItem = (item) => {
  if (item.level == 0) {
    // 省
    changeResult.provinceCode = item.code;
    changeResult.provinceName = item.name;
  } else if (item.level == 1) {
    // 市
    changeResult.cityCode = item.code;
    changeResult.cityName = item.name;
  } else {
    // 区
    changeResult.countyCode = item.code;
    changeResult.countyName = item.name;
    changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`;
    show.value = false;
    emits("change", changeResult);
  }
};

// 实现计算属性： 获取当前显示的地区数组

const currList = computed(() => {
  let list = cityData.value;
  // TODO 根据点击的省份城市获取对应的列表
  if (changeResult.provinceCode && changeResult.provinceName) {
    list = list.find((p) => p.code == changeResult.provinceCode).areaList;
  }
  if (changeResult.cityCode && changeResult.cityName) {
    list = list.find((c) => c.code == changeResult.cityCode).areaList;
  }
  return list;
});
</script>

<template>
  <div class="city" ref="target">
    <div class="select" @click="open">
      <span class="placeholder" v-if="!fullLocation">{{
        placeholder || "请选择配送地址"
      }}</span>
      <span class="value" v-else>{{ fullLocation }}</span>
      <i class="iconfont icon-down"></i>
    </div>
    <div class="option" v-if="show">
      <span
        class="ellipse"
        @click="changeItem(item)"
        v-for="item in currList"
        :key="item.code"
        >{{ item.name }}</span
      >
    </div>
  </div>
</template>

<style lang='less' scoped>
.city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 10px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #666;
      user-select: none;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      user-select: none;
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>